package com.controller;

import com.core.contant.BusinessExceptionCode;
import com.core.exception.BusinessException;
import com.core.exception.InvalidRequestException;
import com.core.exception.ResourceNotFoundException;
import com.core.log.ActionLoggerImpl;
import com.core.platform.RuntimeEnvironment;
import com.core.platform.RuntimeSettings;
import com.core.util.ExceptionUtils;
import com.core.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * Created by dawn on 2017/11/3.
 */
//@ComponentScan(basePackages = {"com.core.platform.RuntimeSettings"})
public class BaseController {

//    private final ActionLoggerImpl actionLogger = ActionLoggerImpl.get();
//
//    @Autowired
//    private RuntimeSettings runtimeSettings;
//
//    @ExceptionHandler
//    @ResponseStatus(HttpStatus.OK)
//    @ResponseBody
//    public BaseResponse error(Throwable e) {
//        return createErrorResponse(e, HttpStatus.INTERNAL_SERVER_ERROR);
//    }
//
//    @ExceptionHandler(ResourceNotFoundException.class)
//    @ResponseStatus(HttpStatus.OK)
//    @ResponseBody
//    public BaseResponse notFound(ResourceNotFoundException e) {
//        return createErrorResponse(e, HttpStatus.NOT_FOUND);
//    }
//
//    @ExceptionHandler(InvalidRequestException.class)
//    @ResponseStatus(HttpStatus.OK)
//    @ResponseBody
//    public BaseResponse validationError(InvalidRequestException e) {
//        return createErrorResponse(e, HttpStatus.BAD_REQUEST);
//    }
//
//    @ExceptionHandler(MethodArgumentNotValidException.class)
//    @ResponseStatus(HttpStatus.OK)
//    @ResponseBody
//    public BaseResponse validationError(MethodArgumentNotValidException e) {
//        return createErrorResponse(e, HttpStatus.BAD_REQUEST);
//    }
//
//    @ExceptionHandler(BusinessException.class)
//    @ResponseStatus(HttpStatus.OK)
//    @ResponseBody
//    public BaseResponse businessError(BusinessException e) {
//        return createErrorResponse(e);
//    }
//
//    protected BusinessException buildBusinessException(BusinessExceptionCode exceptionCode) {
//        return new BusinessException(exceptionCode.getErrorCode(), exceptionCode.getErrorMsg());
//    }
//
//    private BaseResponse createErrorResponse(Throwable e, HttpStatus httpStatus) {
//        BaseResponse baseResponse = new BaseResponse();
//        baseResponse.setStatusCode(String.valueOf(httpStatus.value()));
//        baseResponse.setMessage(httpStatus.name());
//        setErrorMessage(baseResponse, e);
//        setElapsedTime(baseResponse);
//
//        return baseResponse;
//    }
//
//    private BaseResponse createErrorResponse(BusinessException e) {
//        BaseResponse baseResponse = new BaseResponse();
//        baseResponse.setStatusCode(e.getStatusCode());
//        if (!RuntimeEnvironment.PRODUCT.equals(runtimeSettings.getEnvironment())) {
//            baseResponse.setStackTrace(ExceptionUtils.stackTrace(e));
//        }
//
//        baseResponse.setMessage(e.getBusinessMessage());
//        setElapsedTime(baseResponse);
//
//        return baseResponse;
//    }
//
//    private void setErrorMessage(BaseResponse baseResponse, Throwable e) {
//        if (!RuntimeEnvironment.PRODUCT.equals(runtimeSettings.getEnvironment())) {
//            baseResponse.setStackTrace(ExceptionUtils.stackTrace(e));
//        }
//
//        if (StringUtils.hasText(e.getMessage()))
//            baseResponse.setMessage(e.getMessage());
//    }
//
//    protected <T extends BaseResponse> void setElapsedTime(T response) {
//        response.setElapsedTime(calElapsedTime());
//    }
//
//    protected long calElapsedTime() {
//        if (actionLogger.currentActionLog() != null) {
//            return System.currentTimeMillis() - actionLogger.currentActionLog().getRequestDate().getTime();
//        } else {
//            return 0;
//        }
//    }
}
